@@include("./common/_menu.js")
@@include("./common/_popWindow.js")
var columnWidth = 260;
var gutterWidth = 46;
var length = isMobile?40:54;
if(isMobile){
	var proportion = $(window).width()/375;
	columnWidth = Math.floor(154*proportion);
	gutterWidth = Math.floor(19*proportion);
}
 function item_masonry(){ 
	$('.products-box  img').load(function(){ 
		$('.infinite-scroll').masonry({ 
			itemSelector: '.item',
			columnWidth:columnWidth,
			gutterWidth:gutterWidth								
		});		
	});
		
	$('.infinite-scroll').masonry({ 
		itemSelector: '.item',
		columnWidth:columnWidth,
		gutterWidth:gutterWidth								
	});	
}
item_masonry();
$(".infinite-scroll").infinitescroll({
	navSelector: ".recommend-more",
	nextSelector: ".recommend-more a",
	itemSelector: ".item",
	requestUrl: "./lib/recommend.json?type=home&categor_id=1&page=",
	
	loading:{
		img: "images/masonry_loading_1.gif",
		msgText: ' ',
		finishedMsg: '',
		render: function(resData,opts){
			var data = "";
			try{
                var temp = JSON.parse(resData);
                if(temp.code == 200) {
                    if(opts.state.currPage >= temp.data.last_page){
                        opts.state.isFinished = true;
                    }
                    $.each(temp.data.data,function(i,item){
						var price = item.current_price ? item.current_price : item.original_price;
                        var brand = 'Brand ranking:Brand ranking:Brand ranking:Brand ranking:Brand rankingBrand rankingBrand rankingBrand ranking';
						if(brand.length > length){
							brand = brand.slice(0,length) + '...';
						} 
						data += '<div class="products-box item"><a href="'+item.href1+'" class="products-box-img"><span class="img-show"><img src="'+item.cover+'" alt=""></span></a><div class="products-box-tips"><h4><a href="'+item.href2+'" class="descrption">'+item.name+'</a><span></span></h4><div class="tips"><span>'+ brand +'<a>NO.'+item.ranking_rank+'</a></span><a class="price">$'+price+'</a></div></div></div>';  
                    })
                }
            }catch(e){}
            return data;
		},
		finished: function(opts){
			if(opts.state.isFinished){ //结束事件
				$(".recommend-more").remove();
				$("#infscr-loading").hide();
				$(window).unbind('.infscr');
			}
		}	
	},errorCallback:function(res){ 
		console.log(res);
	}
	
},function(newElements){
	var $newElems = $(newElements);
	$('.infinite-scroll').masonry('appended', $newElems, false);
	$newElems.fadeIn();
	item_masonry();
	return;
});
$(".search-mobile-box-button").click(function(){
	if($(this).hasClass("hov")){
		$(this).removeClass("hov");
		$(this).next(".search-mobile-box-list").hide();
	}else{
		$(".search-mobile-box-button").removeClass("hov");
		$(".search-mobile-box-list").hide();
		$(this).addClass("hov");
		$(this).next(".search-mobile-box-list").show();
	}
})
$(".filter-box h3").on("click",function(){
	if($(this).hasClass('hov')){
		$(this).removeClass("hov");
		$(this).next().hide();
	}else{
		$(this).addClass("hov");
		$(this).next().show();
	}	
})
$('#filter-price-set').on("click",function(){
	var min = $("#filter-min").val();
	var max = $("#filter-max").val();
	window.open('http://www.baidu.com?min='+min+'&max='+max);
})
$(".filter-checkbox-list li").click(function(){
	var obj = $(".filter-checkbox-list input");
	var obj_index = $(".filter-checkbox-list li").index(this);
	window.open('http://www.baidu.com?data-id='+obj[obj_index].value);
})
$(".filter").click(function(){
	$(".nav-shadow").removeClass("hide");
	$(".filter-box").removeClass("hide");
})
$(".filter-box .close").on("click",function(){
	$(".nav-shadow").addClass("hide");
	$(".filter-box").addClass("hide");
})
$(".cancel-pop").on("click",function(){
	$(".filter-box").removeClass("hide");
	$(".filter-pop-box").addClass("hide");
})
$("#filter-search-brand").on("click",function(){
	$(".filter-box").addClass("hide");
	$(".nav-shadow").removeClass("hide");
	$(".filter-pop-box-brand").removeClass("hide");
})
$("#filter-search-store").on("click",function(){
	$(".filter-box").addClass("hide");
	$(".nav-shadow").removeClass("hide");
	$(".filter-pop-box-store").removeClass("hide");
})
$(".filter-pop-box-brand .filter-pop-box-option .select-filter").on('click',function(){
	$(this).addClass("hov").siblings("span").removeClass("hov");
	var loves = $(".filter-pop-box-brand .filter-pop-box-con input");
	for (var i = 0; i < loves.length; i++) {
		loves[i].checked = true;
	}
})
$(".filter-pop-box-brand .filter-pop-box-option .unselect-filter").on('click',function(){
	$(this).addClass("hov").siblings("span").removeClass("hov");
	var loves = $(".filter-pop-box-brand .filter-pop-box-con input");
	for (var i = 0; i < loves.length; i++) {
		loves[i].checked = false;
	}
	
})
$(".filter-pop-box-brand .apply-pop").on("click",function(){
	var brand = $(".filter-pop-box-brand .filter-pop-box-con input");
	var check_val = [];
	for (var i = 0; i < brand.length; i++) {
		if (brand[i].checked) {
			check_val.push(brand[i].value);
		}
	}
	check_val = check_val.join(",");
	window.open('http://www.baidu.com?data-id='+check_val);
})
$(".filter-pop-box-store .filter-pop-box-option .select-filter").on('click',function(){
	$(this).addClass("hov").siblings("span").removeClass("hov");
	var loves = $(".filter-pop-box-store .filter-pop-box-con input");
	for (var i = 0; i < loves.length; i++) {
		loves[i].checked = true;
	}
})
$(".filter-pop-box-store .filter-pop-box-option .unselect-filter").on('click',function(){
	$(this).addClass("hov").siblings("span").removeClass("hov");
	var loves = $(".filter-pop-box-store .filter-pop-box-con input");
	for (var i = 0; i < loves.length; i++) {
		loves[i].checked = false;
	}
})
$(".filter-pop-box-store .apply-pop").on("click",function(){
	var store = $(".filter-pop-box-store .filter-pop-box-con input");
	var check_val_store = [];
	for (var i = 0; i < store.length; i++) {
		if (store[i].checked) {
			check_val_store.push(store[i].value);
		}
	}
	check_val_store = check_val_store.join(",");
	window.open('http://www.baidu.com?data-id='+check_val_store);
})
$(".view-more").on("click",function(){
	if($(this).hasClass("down")){
		$(this).parent().addClass("p-ellipsis");
		$(this).removeClass("down").html("View More<i></i>");
	} else {
		$(this).addClass("down").html("Roll Up<i></i>");
		$(this).parent().removeClass("p-ellipsis");
	}
})